﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;



namespace H2.NET.Web.Pages
{
    public partial class TabFormul : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }
                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

         public bool IsHaveAttachment
        {
            get
            {
                return ViewState["IsHaveAttachment"] == null ? false : (bool)ViewState["IsHaveAttachment"];
            }
            set
            {
                ViewState["IsHaveAttachment"] = value;
            }
        }

        //private string[] P_Plant
        //{
        //    get
        //    {
        //        if (StoreAPI["P_Plant~" + Key] == null)
        //        {
        //            IList<m_Plant> dlts = (m_Plant.status == 1 & m_Plant.categary == "P").List<m_Plant>();
        //            string[] p_plant = new string[dlts.Count+1];
        //            if (TaskAPI.Process == "PKG Sefi Code BOM Application")
        //            {
        //                p_plant[0] = "K15P";
        //            }
        //            else
        //            {
        //                p_plant[0] = "";
        //            }
                    
        //            for (int i = 0; i < dlts.Count; i++)
        //            {
        //                p_plant[i+1] = dlts[i].PlantName;
        //            }

        //            StoreAPI["P_Plant~" + Key] = p_plant;
        //        }

        //        return (string[])StoreAPI["P_Plant~" + Key];
        //    }
        //}

       
        

        bool flg_Tech;
        bool flg_pur;
        bool flg_pln;
        bool flg_new;
        bool flg_prd;
        bool flg_mkt;
        bool flg_revision;
        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            
            #region FORMUL
            if (Status.Category == "FORMUL")
            {
                bool flg = Status.Technical == "C" && TaskAPI.IsAllowSubmit;
                gv_Formul.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 80, Align.Left, false, "No", InputType.None, null));
                gv_Formul.DynamicColumn.Add(new BoundColumn2("01", "SAP CODE</br>(创建SEFI Code时Copy)", BoundColumnType2.TextBox, 300, Align.Left, flg, "SAP_CODE", InputType.None, null));
                gv_Formul.DynamicColumn.Add(new BoundColumn2("02", "SEFI Code</br>(BPCS Code)", BoundColumnType2.TextBox, 200, Align.Left, flg, "BPCS_Code", InputType.None, null));
                gv_Formul.DynamicColumn.Add(new BoundColumn2("03", "Description 1(EN.)</br>（原英文描述后面加(YYMM)）", BoundColumnType2.TextBox, 300, Align.Left, flg, "Description_EN", InputType.None, null));
                gv_Formul.DynamicColumn.Add(new BoundColumn2("04", "Description 2(ZH.)</br>（原中文描述后面加(YYMM)）", BoundColumnType2.TextBox, 400, Align.Left, flg, "Description_ZH", InputType.None, null));
                gv_Formul.DynamicColumn.Add(new BoundColumn2("05", "Density", BoundColumnType2.TextBox, 100, Align.Left, flg, "Density", InputType.None, null));
                gv_Formul.Width = 1000;
                SetEnable(flg, gv_Formul_Add);
                SetEnable(flg, gv_Formul_Delete);
                //SetVisible(gv_Plg.Enabled && gv_Plg.Visible, gv_Plg_Add, gv_Plg_Delete);
                if (!Page.IsPostBack)
                {
                    Formul_P17.Visible = true;
                    gv_Formul.Key = Key;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Material_Formul> formul_s = new List<mdc_Material_Formul>();
                        for (int i = 0; i < 12; i++)
                        {
                            formul_s.Add(new mdc_Material_Formul());
                            formul_s[i].No = i + 1;
                        }
                        gv_Formul.DataSource = formul_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Material_Formul> formul_s = (mdc_Material_Formul.process == TaskAPI.Process & mdc_Material_Formul.incident == TaskAPI.Incident).List<mdc_Material_Formul>();
                        if (formul_s.Count == 0)
                        {
                            for (int i = 0; i < 12; i++)
                            {
                                formul_s.Add(new mdc_Material_Formul());
                                formul_s[i].No = i + 1;
                            }
                        }
                        gv_Formul.DataSource = formul_s;
                    
                    }
                    #endregion

                }
            }
            #endregion

        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Formul_Add":
                    List<mdc_Material_Formul> formul_s = (List<mdc_Material_Formul>)gv_Formul.DataSource;
                    mdc_Material_Formul formul = new mdc_Material_Formul();
                    //formul.No = formul_s.Count + 1;
                    formul_s.Insert(gv_Formul.ChangedRowIndex == -1 || gv_Formul.ChangedRowIndex >= formul_s.Count ? formul_s.Count : gv_Formul.ChangedRowIndex, formul);
                    //gv_Formul.DataSource = formul_s;
                    //gv_Formul.DataBind();
                    for (int i = 0; i < formul_s.Count; i++)
                    {
                        formul_s[i].No = (i + 1);
                    }
                    gv_Formul.DataSource = formul_s;
                    gv_Formul.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Formul_Delete":
                    List<mdc_Material_Formul> formul_s = (List<mdc_Material_Formul>)gv_Formul.DataSource;
                    IList<mdc_Material_Formul> list = (mdc_Material_Formul.process == TaskAPI.Process & mdc_Material_Formul.incident == TaskAPI.Incident).List<mdc_Material_Formul>();
                    if (formul_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    formul_s.RemoveAt(gv_Formul.ChangedRowIndex == -1 || gv_Formul.ChangedRowIndex >= formul_s.Count ? formul_s.Count - 1 : gv_Formul.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Material_Formul formul_1 in list)
                    {
                        if (string.IsNullOrEmpty(formul_1.SAP_CODE))
                        {
                            continue;
                        }
                        foreach (mdc_Material_Formul formul_2 in formul_s)
                        {

                            if (string.IsNullOrEmpty(formul_1.SAP_CODE))
                            {
                                continue;
                            }
                            if (formul_2.ID == formul_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(formul_1);
                        }
                        flag = false;
                    }

                    gv_Formul.DataSource = formul_s;
                    gv_Formul.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region FORMUL
                case "FORMUL":
                    foreach (mdc_Material_Formul formul in (List<mdc_Material_Formul>)gv_Formul.DataSource)
                    {
                        //if (string.IsNullOrEmpty(pkg_fini.FINI_Code))
                        //{
                        //    continue;
                        //}
                        formul.Process = TaskAPI.Process;
                        formul.Incident = TaskAPI.Incident;
                        formul.UserName = UserAPI.UserName;
                        formul.Time = System.DateTime.Now;
                        formul.Status = 0;
                        Storage.Save(formul);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            foreach (mdc_Material_Formul formul in (List<mdc_Material_Formul>)gv_Formul.DataSource)
            {
                if (!string.IsNullOrEmpty(formul.SAP_CODE))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, formul.BPCS_Code , "&nbsp;&nbsp;BPCS Code为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, formul.Description_EN, "&nbsp;&nbsp;英文描述为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, formul.Description_ZH, "&nbsp;&nbsp;中文描述为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, formul.Density, "&nbsp;&nbsp;密度为必填"));
                    

                }
            }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion

    }
}